package com.hoshiicloud.goods.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.utils.BeanUtils;
import com.hoshiicloud.goods.entity.goods.PublicProperties;
import com.hoshiicloud.goods.param.goods.PublicPropertiesAddParam;
import com.hoshiicloud.goods.param.goods.PublicPropertiesQueryParam;
import com.hoshiicloud.goods.param.goods.PublicPropertiesUpdateParam;
import com.hoshiicloud.goods.service.PublicPropertiesService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;


/**
 * @Author:campchen
 * @Date:Created in 下午 04:01 2019/7/26
 * @PACKAGENAME com.hoshiicloud.goods.controller
 * @Description
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/goods/public-properties")
@Api(value = "公共属性", tags = {"公共属性"})
public class PublicPropertiesController {
    @Autowired
    private PublicPropertiesService publicPropertiesService;

    @PostMapping("/getPage")
    @ApiOperation(value = "分页查询公共属性表")
    public Response<Page<PublicProperties>> page(@RequestBody PublicPropertiesQueryParam param) {
        QueryWrapper<PublicProperties> queryWrapper = new QueryWrapper<PublicProperties>();
        queryWrapper.lambda()
                .eq(PublicProperties::getCompanyId, param.getCompanyId())
                .eq(StringUtils.isNotBlank(param.getPropertyName()), PublicProperties::getPropertyName, param.getPropertyName())
                .like(StringUtils.isNotBlank(param.getPropertyValue()), PublicProperties::getPropertyValue, param.getPropertyValue())
                .eq(PublicProperties::getDeleted,0);
        String key = "PublicPropertiesQueryParam{" + param.toString() + "},Page{pageNumber=" + param.getPageNumber() + ",pageSize=" + param.getPageSize() + "}";
        Page<PublicProperties> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));
        return Response.data(publicPropertiesService.getPage(page, queryWrapper, key));
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询公共属性表")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response queryById(@PathVariable("id") Long id) {
        return Response.data(publicPropertiesService.getById(id));
    }

    @PostMapping("/save")
    @ApiOperation(value = "新增公共属性表")
    public Response save(@RequestBody PublicPropertiesAddParam param) {
        // Bean数据转换
        PublicProperties publicProperties;
        try {
            publicProperties = BeanUtils.covertType(param, PublicProperties.class);
        } catch (Exception e) {
            log.error("Bean转换异常", e);
            return Response.failed("Bean转换异常");
        }
        return Response.data(publicPropertiesService.save(publicProperties));
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改公共属性表")
    public Response update(@RequestBody @Valid PublicPropertiesUpdateParam param) {
        // Bean数据转换
        PublicProperties publicProperties;
        try {
            publicProperties = BeanUtils.covertType(param, PublicProperties.class);
        } catch (Exception e) {
            log.error("Bean转换异常", e);
            return Response.failed("Bean转换异常");
        }
        return Response.data(publicPropertiesService.updateById(publicProperties));
    }


    @GetMapping("/dropdownBox")
    @ApiOperation(value = "获取下拉框数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "companyId",value = "租户Id",dataType = "Long",example = "4654654665"),
            @ApiImplicitParam(name = "propertyName",value = "属性名",dataType = "String",example = "参数类型")})
    public Response<List<String>> dropdownBox(@RequestParam("companyId")  Long companyId, @RequestParam("propertyName") String propertyName) {
        String key = "DropdownBoxQuery{companyId="+companyId+",propertyName="+propertyName+"}";
        return Response.data(publicPropertiesService.queryDropdownBox(companyId,propertyName,key));
    }
}
